package com.chaopei.znpk.controller;



import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.chaopei.znpk.entity.Classroom;
import com.chaopei.znpk.service.ClassroomService;
import com.chaopei.znpk.util.EasyExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * (Classroom)表控制层
 *
 * @author kjx
 * @since 2022-05-26 09:53:23
 */
@CrossOrigin
@RestController
@RequestMapping("/classroom")
public class ClassroomController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ClassroomService classroomService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public R selectAll() {
        return success(this.classroomService.list());
    }

    /**
     * 新增数据
     *
     * @param classroom 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public R insert(@RequestBody Classroom classroom) {
        classroom.setCid(System.currentTimeMillis()+"");
        return success(this.classroomService.save(classroom));
    }

    /**
     * 教室信息导入
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/excelImport")
    public R excelImport(MultipartFile file) throws IOException {
        List<Classroom> rooms = EasyExcelUtil.read(file.getInputStream(), Classroom.class);
        classroomService.saveBatch(rooms);
        return R.ok("导入成功");
    }

    /**
     * 导出教师信息模板
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

        EasyExcelUtil.download(response,Classroom.class, new ArrayList<>(), "教室信息表");
    }

}

